package twitterInfoEngine;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import dao.TweetKeywordsDAO;
import daoImp.TweetKeywordsDAOImp;

import processors.ProcessorController;

import twitter4j.Tweet;
import util.KeywordUtil;

public class KeywordsAnalyzer {
	private ProcessorController proceesorController = new ProcessorController();
	private TweetKeywordsDAO tweetKeywordsDAO = TweetKeywordsDAOImp.getInstance();
	
	public void analyze(ArrayList<Tweet> tweets, String trend , Date date) {
		ArrayList<String> tweetContents = extracrtContentList(tweets);
		Map<String, Integer> frequency = proceesorController.process(tweetContents);
		Map<String, Integer> oldFrequency = tweetKeywordsDAO.getFrequencyByTrend(trend, date);
		Map<String, Integer> newFrequency = KeywordUtil.converge(frequency, oldFrequency);
		tweetKeywordsDAO.updateKeywords(newFrequency, trend, date);
	}


	private ArrayList<String> extracrtContentList(ArrayList<Tweet> tweets) {
		ArrayList<String> contentList = new ArrayList<String>();
		for(Tweet tweet : tweets){
			contentList.add(tweet.getText());
		}
		return contentList;
	}

}
